using System;
using System.Collections.Generic;
using System.Text;

namespace org.ngwt.user.client.rpc.impl
{
    /**
     * Base class for the client and server serialization streams. This class
     * handles the basic serialization and deserialization formatting for primitive
     * types since these are common between the client and the server.
     */
    public abstract class AbstractSerializationStream
    {
        public static int SERIALIZATION_STREAM_FLAGS_NO_TYPE_VERSIONING = 1;
        public static int SERIALIZATION_STREAM_VERSION = 3;

        /**
         * The last legacy stream version which does not use a
         * {@link com.google.gwt.user.server.rpc.SerializationPolicy SerializationPolicy}.
         */
        public static int SERIALIZATION_STREAM_VERSION_WITHOUT_SERIALIZATION_POLICY = 2;

        private int flags = 0;

        private int version = SERIALIZATION_STREAM_VERSION;

        public void addFlags(int flags)
        {
            this.flags |= flags;
        }

        public int getFlags()
        {
            return flags;
        }

        public int getVersion()
        {
            return version;
        }

        public void setFlags(int flags)
        {
            this.flags = flags;
        }

        public Boolean shouldEnforceTypeVersioning()
        {
            return (flags & SERIALIZATION_STREAM_FLAGS_NO_TYPE_VERSIONING) == 0;
        }

        /**
         * Returns <code>true</code> if this stream encodes information which can be
         * used to lookup a {@link com.google.gwt.user.server.rpc.SerializationPolicy}.
         * 
         * @return <code>true</code> if this stream encodes information which can be
         *         used to lookup a <code>SerializationPolicy</code>
         */
        protected Boolean hasSerializationPolicyInfo()
        {
            return getVersion() > SERIALIZATION_STREAM_VERSION_WITHOUT_SERIALIZATION_POLICY;
        }

        protected void setVersion(int version)
        {
            this.version = version;
        }
    }
}
